package com.wz.dhs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {

    public static final String CUSTOMER_THREAD_PREFIX = "goose-thread";

    /**
     * @description: 自定义线程池
     * @param:
     * @return:
     * @author: xia
     * @time: 2022/2/12 11:36
     */
    @Bean(name = "customPool")
    public Executor gooseThreadPool(){
        ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
        //核心线程池数量
        threadPoolExecutor.setCorePoolSize(10);
        //最大线程池数量
        threadPoolExecutor.setMaxPoolSize(10);
        //阻塞队列长度
        threadPoolExecutor.setQueueCapacity(20);
        //拒绝策略，当到达最大线程池容量的时候，使用调用者的线程来继续执行
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolExecutor.setThreadNamePrefix(CUSTOMER_THREAD_PREFIX);
        return threadPoolExecutor;

    }

}
